<?php
/**
*
* 功能描述（食品管理控制器）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class CtrlFood extends CtrlBase
{
	public $paramCheck = array(
		"index" => array(
			"type" => array("GET", "0", "intval", "positive"),
			"type2" => array("GET", "0", "intval", "positive"),
			"cheap" => array("GET", "0", "intval", "positive"),
			"page" => array("GET", "1", "intval", "positive"),
		),
		"detail" =>array(
			"id" => array("GET", "must", "intval", "positive"),
			"version" => array("GET", "0", "intval", "positive"),
			"page" => array("GET", "1", "intval", "positive"),
		),
	);

	function Index()
	{
		//left add:参数校验
		$this->BindSession();
		$where = "1";
		$type = $this->params['type'];
		$type2 = $this->params['type2'];
		$cheap = $this->params['cheap'];
		if($type != 0)
			$where .= " and type = " . $type;
		if($type2 != 0)
			$where .= " and type2 = " . $type2;
		if($cheap == 1)
			$where .= " and discount != 1";
		$obj = $this->GetModel("Food");
		$data = $obj->GetGoodsList($this->params['page'], $where);
		$this->assign("goodsList", $data['data']);
		$this->assign("totalCnt", $data['cnt']);
		$this->assign("pageCnt", $data['pageCnt']);
		$this->assign("curPage", $this->params['page']);
		
		$this->assign("type", $type);
		$this->assign("type2", $type2);
		$this->assign("cheap", $cheap);
		return RT_TPL;
	}

	function Detail()
	{
		$obj = $this->GetModel("Food");
		$id = $this->params['id'];
		$data = $obj->GetGoods($id);
		if(!$data)
		{
			$this->assign("msg", "商品已不存在");
			return RT_MSG;
		}
		$this->assign("goodsInfo", $data);

		$ver = $this->params['version'];
		$this->assign("change", $ver != 0 && $ver != $data['version']);

		$data = $obj->GetPrice("goods_id = $id");
		if(count($data) <= 0)
		{
			$this->assign("msg", "商品未标价");
			return RT_MSG;
		}
		$this->assign("prices", $data);
		$data = $obj->GetInfo($id, 1);
		if(!$data)
		{
			$this->assign("msg", "商品未添加属性");
			return RT_MSG;
		}
		$this->assign("goodsInfo2", $data);

		$obj = $this->GetModel("Comment");
		$data = $obj->GetGoodsComments($id, $this->params['page']);
		$this->assign("curPage", $this->params['page']);
		$this->assign("comments", $data['data']);
		$this->assign("totalCnt", $data['cnt']);
		$this->assign("pageCnt", $data['pageCnt']);
		return RT_TPL;
	}
}

